import 'package:flutter/material.dart';
import 'package:kuaitie/MyDevice.dart';
import 'package:kuaitie/login.dart';
import 'package:kuaitie/register.dart';
import 'package:flutter/services.dart';
import 'API.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  List tabItem = new List<Tab>();
  List listItem = new List<ListTile>();
  bool isLogin;
  TextEditingController textEditingController = new TextEditingController();

  Future<void> showListDialog() async {
    int index = await showDialog<int>(
        context: context,
        builder: (BuildContext context) {
          var child = Column(
            children: <Widget>[
              ListTile(
                title: Text('请选择群组'),
              ),
              Expanded(
                child: ListView.builder(
                    itemCount: 5,
                    itemBuilder: (BuildContext context, int position) {
                      return ListTile(
                        title: Text("群组$position"),
                        onTap: () {},
                      );
                    }),
              ),
            ],
          );
          return Dialog(
            child: child,
          );
        });
    if (index != null) {
      print("点击了,$index");
    }
  }

  List<Tab> gettabItem() {
    Tab tab1 = new Tab(
      text: '个人',
    );
    Tab tab2 = new Tab(
      text: '群组',
    );
    tabItem.add(tab1);
    tabItem.add(tab2);
  }

  List<ListTile> getListTile() {
    ListTile listTile1 = new ListTile(
      leading: Icon(Icons.school),
      title: Text('注销'),
      onTap: () {
        //
      },
    );
    ListTile listTile2 = new ListTile(
      leading: Icon(Icons.school),
      title: Text('我的设备'),
      onTap: () {
        Navigator.push(context,
            MaterialPageRoute(builder: (BuildContext context) => MyDevice()));
      },
    );
    ListTile listTile3 = new ListTile(
      leading: Icon(Icons.school),
      title: Text('清空'),
      onTap: () {},
    );
    ListTile listTile4 = new ListTile(
      leading: Icon(Icons.school),
      title: Text('加入群组'),
      onTap: () {
        showDialog(
            context: context,
            barrierDismissible: true,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('请输入邀请码'),
                content: TextField(
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(16),
                    hintText: "请输入邀请码",
                    errorText: '错误的邀请码',
                    prefixIcon: Icon(Icons.lock),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        width: 1,
                      ),
                      borderRadius: BorderRadius.all(const Radius.circular(5)),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        width: 3,
                        color: Colors.blue,
                      ),
                      borderRadius: BorderRadius.all(const Radius.circular(5)),
                    ),
                    errorBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        width: 3,
                        color: Colors.red,
                      ),
                      borderRadius: BorderRadius.all(const Radius.circular(5)),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        width: 3,
                        color: Colors.red,
                      ),
                      borderRadius: BorderRadius.all(const Radius.circular(5)),
                    ),
                  ),
                  controller: textEditingController,
                ),
                actions: <Widget>[
                  FlatButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text('确定')),
                  FlatButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text('取消')),
                ],
              );
            });
      },
    );
    ListTile listTile5 = new ListTile(
      leading: Icon(Icons.school),
      title: Text('群组管理'),
      onTap: () {
        showListDialog();
      },
    );
    listItem.add(listTile1);
    listItem.add(listTile2);
    listItem.add(listTile3);
    listItem.add(listTile4);
    listItem.add(listTile5);
  }

  TabController tabController;

  void initState() {
    super.initState();
    tabController = new TabController(length: 2, vsync: this);
    gettabItem();
    getListTile();
  }

  Widget checkState(bool isLogin) {
    if (isLogin) {
      return Main();
    } else {
      return Login1();
    }
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: Text('这是一个很大的饼'),
        leading: Builder(
          builder: (context){
            return IconButton(
              icon: Icon(Icons.menu),
              onPressed: () {
                Scaffold.of(context).openDrawer();
              },
            );
          },
        ),
      ),
      body: checkState(false),
      drawer: drawer(),
      floatingActionButton: PopupMenuButton(
        icon: Icon(Icons.add),
        onSelected: (result)async {
          if (result == 1) {} else if (result == 2) {} else if (result == 3){
              //Clipboard.setData(ClipboardData(text:"123321"));
              /*ClipboardData data=await Clipboard.getData(Clipboard.kTextPlain);
              if(data!=null){
                print(data.text);
              }*/
           print(await Clip.isEuropeUser());
          }
        },
        itemBuilder: (BuildContext context) =>
        <PopupMenuEntry>[
          const PopupMenuItem(
            child: Text('从剪切板复制'),
            value: 3,
          ),
          const PopupMenuItem(
            child: Text('图片'),
            value: 2,
          ),
          const PopupMenuItem(
            child: Text('文件'),
            value: 1,
          ),
        ],
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    );
  }
  Widget drawer(){
   return new Drawer(
      child: MediaQuery.removePadding(
        context: context,
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 2,
              child: Container(
                decoration: BoxDecoration(color: Colors.black),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      flex: 2,
                      child: Container(
                        child: ClipOval(
                          child: SizedBox(
                            width: 40,
                            height: 40,
                            child: Image.asset(
                              'assets/image1.jpg',
                              color: Colors.white,
                            ),
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 3,
                      child: Center(
                        child: Text(
                          '用户名',
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              flex: 8,
              child: Column(
                children: <Widget>[
                  ListView(
                    children: listItem,
                    shrinkWrap: true,
                  )
                ],
              ),
            )
          ],
        ),
        removeTop: true,
      ),
    );
  }
}

class Person extends StatefulWidget {
  PersonState createState() => new PersonState();
}

class PersonState extends State<Person> {
  Widget build(BuildContext context) {
    return ListView.builder(itemBuilder: (BuildContext context, int position) {
      return Container(
        padding: EdgeInsets.fromLTRB(16, 10, 16, 10),
        child: SizedBox(
          height: MediaQuery
              .of(context)
              .size
              .height / 5,
          child: Card(
            child: Column(
              children: <Widget>[
                Expanded(
                  flex: 8,
                  child: Container(
                    width: MediaQuery
                        .of(context)
                        .size
                        .width * 8 / 10,
                    padding: EdgeInsets.fromLTRB(16, 10, 16, 5),
                    decoration: BoxDecoration(
                        color: Colors.grey,
                        border: Border.all(width: 1.0, color: Colors.red),
                        borderRadius: BorderRadius.all(Radius.circular(5.0))),
                    child: Center(
                      child: Text('粘贴区域'),
                    ),
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Container(
                    padding: EdgeInsets.fromLTRB(16, 2, 16, 2),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: Container(
                            padding: EdgeInsets.fromLTRB(16.0, 1, 16, 5),
                            child: Center(
                              child: Icon(
                                Icons.delete,
                                size: 22,
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 3,
                          child: Container(
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  flex: 2,
                                  child: Center(
                                    child: Text('时间'),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: Text('机型'),
                                )
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
      );
    });
  }
}

class Login1 extends StatelessWidget {
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Positioned(
          left: 50,
          child: RaisedButton(
            child: Text('登录'),
            onPressed: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (BuildContext context) => Login()));
            },
          ),
        ),
        Positioned(
          right: 50,
          child: RaisedButton(
            child: Text('注册'),
            onPressed: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (BuildContext context) => Register()));
            },
          ),
        )
      ],
    );
  }
}

class Main extends StatefulWidget {
  MainState createState() => MainState();
}

class MainState extends State<Main> {
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Container(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: TabBar(
                unselectedLabelColor: Colors.black,
                indicatorColor: Colors.black,
                labelColor: Colors.black,
                tabs: <Widget>[
                  new Tab(
                    text: '个人',
                  ),
                  new Tab(
                    text: "群组",
                  )
                ],
                //controller:tabController,
              ),
            ),
            Expanded(
              flex: 9,
              child: TabBarView(
                // controller: tabController,
                children: <Widget>[
                  new Person(),
                  new Person(),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
